package org.gege.com.controller;

import org.gege.com.bean.FtlPage;
import org.gege.dao.HunterPortraitDao;
import org.gege.dbbean.HunterPortrait;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FtlController {

    @RequestMapping("/w/{pageNo}")
    public String index(ModelMap map, @PathVariable("pageNo") Integer pageNo) {
        pageNo = pageNo == null ? 0 : pageNo;
        // 加入一个属性，用来在模板中读取
        PageRequest pageRequest = new PageRequest(pageNo, 30);
        Page<HunterPortrait> page = hunterPortraitDao.findAllByOrderByCreateTimeDesc(pageRequest);
        map.addAttribute("page", convert(page));
        map.addAttribute("list", page.getContent());
        
        return "welcome";
    }
    
    private <T> FtlPage<T> convert(Page<T> page) {
        FtlPage<T> fp = new FtlPage<>();
        fp.setTotalPages(page.getTotalPages());
        fp.setPageNo(page.getNumber());
        fp.setPageSize(page.getSize());
        fp.setIsFirstPage(page.isFirst());
        fp.setIsLastPage(page.isLast());
        return fp;
    }

    @Autowired
    private HunterPortraitDao hunterPortraitDao;
}
